package com.walixiwa.m3udownloader.utils;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.walixiwa.m3udownloader.DownloadListener;
import com.walixiwa.m3udownloader.adapter.DownloadAdapter;

import jaygoo.library.m3u8downloader.bean.M3U8Task;

public class TaskShowManager {
    private Activity activity;
    private RecyclerView mRecyclerView;
    private DownloadAdapter adapter = new DownloadAdapter(DownloadManager.getTasks());

    private Handler handler = new Handler(Looper.getMainLooper());

    public TaskShowManager with(Activity activity) {
        this.activity = activity;
        return this;
    }

    public TaskShowManager setRecyclerView(RecyclerView recyclerView) {
        this.mRecyclerView = recyclerView;
        this.mRecyclerView.setLayoutManager(new GridLayoutManager(activity,1));
        this.mRecyclerView.setAdapter(adapter);
        this.adapter.notifyDataSetChanged();
        DownloadManager.setListener(downloadListener);
        DownloadManager.startNext();
        return this;
    }


    private DownloadListener downloadListener = new DownloadListener() {
        @Override
        public void onDownloadError(final M3U8Task m3U8Task, final Throwable throwable) {
            int position = getTaskPosition(m3U8Task);
            if (position != -1) {
                final DownloadAdapter.MyViewHolder viewHolder = getViewHolder(position);
                if (viewHolder != null) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            viewHolder.mTvInfo.setText(String.format("下载错误:%s", throwable.toString()));
                            viewHolder.mTvSpeed.setText(m3U8Task.getFormatSpeed());
                            viewHolder.mProgressBar.setProgress((int) (m3U8Task.getProgress() * 100));
                        }
                    });

                }
            }
        }

        @Override
        public void onDownloadItem(final M3U8Task m3U8Task, long l, final int total, final int count) {
            int position = getTaskPosition(m3U8Task);
            if (position != -1) {

                final DownloadAdapter.MyViewHolder viewHolder = getViewHolder(position);
                if (viewHolder != null) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            viewHolder.mTvInfo.setText(String.format("%s / %s", count, total));
                            viewHolder.mTvSpeed.setText(m3U8Task.getFormatSpeed());
                            viewHolder.mProgressBar.setProgress((int) (m3U8Task.getProgress() * 100));
                        }
                    });
                }
            }
        }

        @Override
        public void onDownloadPause(final M3U8Task m3U8Task) {
            int position = getTaskPosition(m3U8Task);
            if (position != -1) {

                final DownloadAdapter.MyViewHolder viewHolder = getViewHolder(position);
                if (viewHolder != null) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            viewHolder.mTvInfo.setText("下载暂停");
                            viewHolder.mTvSpeed.setText(m3U8Task.getFormatSpeed());
                            viewHolder.mProgressBar.setProgress((int) (m3U8Task.getProgress() * 100));
                        }
                    });
                }
            }
        }

        @Override
        public void onDownloadPending(final M3U8Task m3U8Task) {
            int position = getTaskPosition(m3U8Task);
            if (position != -1) {

                final DownloadAdapter.MyViewHolder viewHolder = getViewHolder(position);
                if (viewHolder != null) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            viewHolder.mTvInfo.setText("等待下载");
                            viewHolder.mTvSpeed.setText(m3U8Task.getFormatSpeed());
                            viewHolder.mProgressBar.setProgress(0);
                        }
                    });
                }
            }
        }

        @Override
        public void onDownloadPrepare(final M3U8Task m3U8Task) {
            int position = getTaskPosition(m3U8Task);
            if (position != -1) {
                final DownloadAdapter.MyViewHolder viewHolder = getViewHolder(position);
                if (viewHolder != null) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            viewHolder.mTvInfo.setText("开始准备下载...");
                            viewHolder.mTvSpeed.setText(m3U8Task.getFormatSpeed());
                            viewHolder.mProgressBar.setProgress(0);
                        }
                    });
                }
            }
        }

        @Override
        public void onDownloadProgress(final M3U8Task m3U8Task) {
            int position = getTaskPosition(m3U8Task);
            if (position != -1) {
                Log.e("info", "onDownload: " + (m3U8Task.getProgress() * 100));
                final DownloadAdapter.MyViewHolder viewHolder = getViewHolder(position);
                if (viewHolder != null) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            viewHolder.mTvSpeed.setText(m3U8Task.getFormatSpeed());
                            viewHolder.mProgressBar.setProgress((int) (m3U8Task.getProgress() * 100));
                        }
                    });
                }
            }
        }

        @Override
        public void onDownloadSuccess(final M3U8Task m3U8Task) {
            int position = getTaskPosition(m3U8Task);
            if (position != -1) {
                final DownloadAdapter.MyViewHolder viewHolder = getViewHolder(position);
                if (viewHolder != null) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            viewHolder.mTvInfo.setText("下载完成");
                            viewHolder.mTvSpeed.setText(m3U8Task.getFormatTotalSize());
                            viewHolder.mProgressBar.setProgress(100);
                        }
                    });
                }
            }
        }
    };




    private int getTaskPosition(M3U8Task task) {
        for (int i = 0; i < DownloadManager.getTasks().size(); i++) {
            if (DownloadManager.getTasks().get(i).getUrl().equals(task.getUrl())) {
                return i;
            }
        }
        return -1;
    }



    private DownloadAdapter.MyViewHolder getViewHolder(int position) {
        return (DownloadAdapter.MyViewHolder) mRecyclerView.findViewHolderForLayoutPosition(position);
    }

    public void release() {
        DownloadManager.setListener(null);
        this.downloadListener = null;
    }

    public DownloadAdapter getAdapter() {
        return adapter;
    }
}
